<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\YiNew;
use Illuminate\Support\Facades\Http;
use GuzzleHttp\Client;
use Nesk\Puphpeteer\Puppeteer;
use DOMDocument;

class MakeMpData extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'cc:mp-data';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
       $news = YiNew::query()->where('source',"weixin")->where("views",0)->where("mp_biz","MjM5MjAyNDUyMA==")->get();
       $appmsg_token="1312_DzFdgoCzPjUfLWPsPSHGHNHhrv7YxjUMRn6hQ_YVVIGYa96H6oFl1BHG6Mxnz09yAjYCowbssZD8f5Q9";
       $pass_ticket="MNMkPh7bk1MVjsynqNv9GG3Zh1HerKJ3dvBn%252B1E5MJM%252BidrnbLRv8A%252FNYM7BRfL9";
       $key="daf9bdc5abc4e8d010d9d39163df549817f632369f54e6fb458d32a8fb4eab0f93f4e8e741322e16e62c221128c458c267fd5931d4cfa12e63abb6c1a6b3e09157cbff75101dcbb79f2fc99c066408cba3b7122036954b6fa940330a610b21a83942a9ac715d2af91fae8bde0956b3f8e6bccecfc107c4e2568d9dba1af9e9a8";

       $form = [
            "is_only_read"=>"1",
            "is_temp_url"=> "0",              
            "appmsg_type"=>"9", # 新参数，不加入无法获取like_num
        ];
       foreach($news as $k=>$v){
            $query=[
               "__biz"=>$v['mp_biz'],
                "mid"=>$v['mp_article_mid'],
                "sn"=>$v['mp_sn'],
                "idx"=>$v['mp_idx'],
                "key"=> $yourkey,
                "pass_ticket"=>$pass_ticket,
                "appmsg_token"=>$appmsg_token,
            ];



       /* $user_agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)";

        $cookie = "appmsglist_action_3975454104=card; ua_id=BulSpRyLwIVHcjFFAAAAAAuFm4jxetQoRq-qOxqK6oQ=; wxuin=18503783010187; mm_lang=zh_CN; pgv_pvid=3760479382; _clck=la1z9j|1|ftx|0; xid=a0fa20eb5d4cbd1cd887e15a73d4282d; data_bizuin=3975454104; bizuin=3975454104; data_ticket=RsLGjNp7WtRPhsYf5XHe+wRsHsbJ+bTTZJO83sbZYwK4aPPSo+COGvdLyXp6Pf5T; rand_info=CAESIDhC4YG49w0XqhSgPUEEvshTLsNF4IMLexR2EB8RJuw1; slave_bizuin=3975454104; slave_user=gh_6d7bbc3c44f2; slave_sid=QU9BVV9tUEV6WjBOY3g5R1FmM1Q5SWFqNFV3MWpGdUttclBxRXI2dXRCcTRhcmdLTTU5UTZxTVBpOXFVZ05ya3ZKbW9KeV91QWx2aENWUnRpQjJrRDJlNkVpY0hMNGFrTlpKTlNVV3p5R040Z0hWcUlhdUdHdGFaM1JndWJkVERDbmpxVUw5WFZPblJJUmFU; _clsk=h1i4ee|1741082508648|3|1|mp.weixin.qq.com/weheat-agent/payload/record";

        $key="daf9bdc5abc4e8d07b9ae14c9528f47c466402bbd4c45801bcd9a994b97b6f695c3128531fc1166283668e043e0df6f7b779a105549b6f927fc3ea49d2ad1c3a1e54b6b2bfb2607483be1cb8ab860528e89dca9142c9dab592193e0a5812bfd7c5f891af419cd79f2123f7527a02939dc2921eae813e85cfc553c47d9f482058";
        $pass_ticket="Yt4kzNgzHBQO37q3VdmJDs8omYBfSZQS93Z/JDxqLXNRRFeE8aIidLBTDBt5PsSK";
        $appmsg_token="1312_mdPz3mkq50TD%2FufYlnImgTr-v0tlhIgK-ClqbvkoGDi_mjOzEH8oAMTkR2eCiIHCMLRGZ_pZylE8K6Hb";*/
        foreach($news as $k=>$v){
            $url="https://mp.weixin.qq.com/s?__biz=".$v->mp_biz."&mid=".$v->mp_article_mid."&idx=".$v->mp_idx."&sn=".$v->mp_sn."&chksm=bcd9644e3fde2d3656ef5b12df2a1629c58107df078b1a0582fc7b3875551632a508ea264004&scene=126&sessionid=1741674697&subscene=7&clicktime=1741674700&enterid=1741674700&key=daf9bdc5abc4e8d05fd36fb262b8ec5dfe238bab26bc586fbcd9247b4fd43ea557805bb17aec614bb2f9732b4ce7797ee6d3a6e513e16a7f784dc49d808a32775f20c4cd20e18f35ba70bbc12e97299a1af2451eee9def6e5ae683a0f72c09ea0144544bf291ee7cad496525937ee699b3213bc48a1d9dfaf581d9f29eb5fda9&ascene=0&uin=MTYwOTY1NzYyMA%3D%3D&devicetype=Windows+10+x64&version=63090c2d&lang=zh_CN&countrycode=CN&exportkey=n_ChQIAhIQnv1lDEP5HHYylVzPaQALahLiAQIE97dBBAEAAAAAAN%2BKE5%2B30VoAAAAOpnltbLcz9gKNyK89dVj0zfLAFQYgQWQaK8e%2FJP3Bt1D32YtGEjk9RcUe0PHFLyqNuZK8LcCiq%2F%2FwC53nJ%2FV6cF5ylJoOrKPLdGGMDx43X4%2B91qXRoGruZNC6rehkpvUTn9essYvdVPvW9YKX0yNAYp2udJEbCyfSib9VGdg4J%2FHfBGA6UozxjbmYkgNzQ5t2683hlm1tFUdX9X6TaGJI1BbjX5nOgzmsRAhlAU6%2F9RuO6F9xaYktKz8DRgoVqrykjeCaDPT%2BEGPqE1g%3D&acctmode=0&pass_ticket=mgh4EW9eV%2F%2B3nJQUI7C6UJPUO%2FDUoZSp7V0kpRIIIaEBlGia4LQIMXSYZtGlvx%2Bj&wx_header=1&fasttmpl_type=0&fasttmpl_fullversion=7638361-zh_CN-zip&fasttmpl_flag=1";



             $puppeteer = new Puppeteer; // 新建 Puppeteer 实例

             $browser = $puppeteer->launch(['args'=>['--no-sandbox']]); // 启动无头浏览器
             $page = $browser->newPage(); // 打开新的标签页
           
             $page->tryCatch->goto($url,[
                 'timeout' => 0,
                 'read_timeout' => 60

             ]); // 访问页面
                
             
             $html = $page->content();

             $dom = new DOMDocument();
            

            libxml_use_internal_errors(true);
            //$html = htmlspecialchars($html);
            
            
            $dom->loadHTML('<?xml encoding="UTF-8">' . $html);


            $elements = $dom->getElementsByTagName('span');

           
            $data=[
                'views'=>0,
                'likes'=>0,
                'collections'=>0
            ];


            foreach($elements as $key=>$val){
               
                if (strpos($val->getAttribute('id'), 'js_btm_temp_read_num') !== false) {
                    if(strpos($val->textContent,"万")){
                        $views = (float)$val->textContent * 10000;
                    }else{
                        $views = (float)$val->textContent;
                    }
                    $data['views'] = $views;



                }
                if (strpos($val->getAttribute('id'), 'js_bar_share_btn') !== false) {

                    $data['likes'] = $val->textContent;
                }
                 if (strpos($val->getAttribute('id'), 'js_bar_like_btn') !== false) {

                    $data['collections'] = $val->textContent;
                }
            }

             $v->update($data);

             $browser->close();
           
        }

       }

       

      /*  $data = [
            "is_only_read"=>"1",
            "is_temp_url"=> "0",              
            "appmsg_type"=> "9", # 新参数，不加入无法获取like_num
        ];

         $headers =['cookie' => $cookie,'user_agent'=>$user_agent];

          $client = new Client();
       foreach($news as $k=>$v){

            $queryUrl = "http://mp.weixin.qq.com/mp/getappmsgext";
            $query=[
                 "__biz"=>$v['mp_biz'],
                "mid"=>$v['mp_article_mid'],
                "sn"=>$v['mp_sn'],
                "idx"=>$v['mp_idx'],
                "key"=>$key,
                "pass_ticket"=>$pass_ticket,
                "appmsg_token"=>$appmsg_token,
                //"uni"=>
            ];

              $response = $client->post($queryUrl, [
                'headers' => $headers,
                'form_params' => $data,
                'query' => $query,
            ]);

            $content = json_decode($response->getBody(), true);

             //$response = Http::withHeaders()->post($queryUrl,$query);

            dd($content);

       }*/

    }
}